#!/usr/bin/env python
# -*- coding: utf-8 -*-

import redis
import cPickle
import os
import time

pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
r = redis.Redis(connection_pool=pool)



"""node_rank = dict()
rank_cache = "rank.txt"
expiry = 24 * 3600
current_time = time.time()
user_id = '100000077'
try:
    mtime = os.path.getmtime(rank_cache)
    if (current_time - mtime) > expiry:
        mtime = 0
    else:
        with open(rank_cache) as fp:
            node_rank = cPickle.load(fp)
except OSError:
    mtime = 0
if mtime != 0:
    with open(rank_cache) as fp:
        node_rank = cPickle.load(fp)
else:
    j = 1
    current_rank = 0
    for i in r.zrevrangebyscore('fuwa_rank', '+inf', '-inf', withscores=True):
        if j == 101:
            break
        k = str(j)
        if node_rank.get(k, -1) == -1:
            node_rank[k] = dict(users=set([i[0]]), num=int(i[1]))
        else:
            t = node_rank.get(k)
            if int(t.get('num')) == int(i[1]):
                # if len(t['users']) > 3:
                #    continue
                t['users'].add(i[0])
                # if user_id == i[0]:
                #    current_rank = j
            else:
                j += 1
                k = str(j)
                node_rank[k] = dict(users=set([i[0]]), num=int(i[1]))
    # write to cache file
    with open(rank_cache, "w") as fp:
        cPickle.dump(node_rank, fp)

for i in [str(x) for x in range(1, 100)]:
    if node_rank.get(i):
        for uid in node_rank.get(i)['users']:
            if user_id == uid:
                print i
"""
e = r.zrevrange('fuwa_rank', 0, 99, withscores=True)
for i in e:
    print i